package com.ilock.bean;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Ilockinfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ilock.bean.Ilockinfo
 * @author MyEclipse Persistence Tools
 */
public class IlockinfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(IlockinfoDAO.class);
	// property constants
	public static final String ILOCK_INFO_PHONE_NUMBER = "ilockInfoPhoneNumber";
	public static final String ILOCK_INFO_PASSWORD = "ilockInfoPassword";
	public static final String ILOCK_INFO_TYPE = "ilockInfoType";
	public static final String ILOCK_INFO_MAC = "ilockInfoMac";
	public static final String ILOCK_INFO_UUID = "ilockInfoUuid";
	public static final String ILOCK_INFO_NAME = "ilockInfoName";

	protected void initDao() {
		// do nothing
	}

	public void save(Ilockinfo transientInstance) {
		log.debug("saving Ilockinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Ilockinfo persistentInstance) {
		log.debug("deleting Ilockinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Ilockinfo findById(java.lang.Integer id) {
		log.debug("getting Ilockinfo instance with id: " + id);
		try {
			Ilockinfo instance = (Ilockinfo) getHibernateTemplate().get(
					"com.ilock.bean.Ilockinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public Ilockinfo checkLockInfo(int lockInfoNumber,String password){
		try {
			String queryString = "from Ilockinfo as model where model.ilockInfoNumber"
					+ "="+lockInfoNumber+" and model.ilockInfoPassword='"+password+"'";
			Ilockinfo instance = null;
			if(!getHibernateTemplate().find(queryString).isEmpty()){
				instance = (Ilockinfo) getHibernateTemplate().find(queryString).get(0);
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("find lock info failed", re);
			throw re;
		}
	}

	public List findByExample(Ilockinfo instance) {
		log.debug("finding Ilockinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Ilockinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Ilockinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByIlockInfoPhoneNumber(Object ilockInfoPhoneNumber) {
		return findByProperty(ILOCK_INFO_PHONE_NUMBER, ilockInfoPhoneNumber);
	}

	public List findByIlockInfoPassword(Object ilockInfoPassword) {
		return findByProperty(ILOCK_INFO_PASSWORD, ilockInfoPassword);
	}

	public List findByIlockInfoType(Object ilockInfoType) {
		return findByProperty(ILOCK_INFO_TYPE, ilockInfoType);
	}

	public List findByIlockInfoMac(Object ilockInfoMac) {
		return findByProperty(ILOCK_INFO_MAC, ilockInfoMac);
	}

	public List findByIlockInfoUuid(Object ilockInfoUuid) {
		return findByProperty(ILOCK_INFO_UUID, ilockInfoUuid);
	}

	public List findByIlockInfoName(Object ilockInfoName) {
		return findByProperty(ILOCK_INFO_NAME, ilockInfoName);
	}

	public List findAll() {
		log.debug("finding all Ilockinfo instances");
		try {
			String queryString = "from Ilockinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Ilockinfo merge(Ilockinfo detachedInstance) {
		log.debug("merging Ilockinfo instance");
		try {
			Ilockinfo result = (Ilockinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Ilockinfo instance) {
		log.debug("attaching dirty Ilockinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Ilockinfo instance) {
		log.debug("attaching clean Ilockinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void update(Ilockinfo instance){
		log.debug("update Ilockinfo instance");
		try {
			getHibernateTemplate().update(instance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public static IlockinfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IlockinfoDAO) ctx.getBean("IlockinfoDAO");
	}
}